package com.leetcode.code;

import java.util.ArrayList;
import java.util.List;

/**
 * Description:
 *
 * @author zkingcobra
 * Date: 2020/6/12 4:49 下午
 */
public class LeetCode0006Java {
    public static void main(String[] args) {
        Solution solution = new Solution();
        System.out.println(solution.convert("**********************", 4));
    }

    static class Solution {

        /**
         * 方法一：按行排序
         *
         * @param numRows > 2
         */
        public String convert(String s, int numRows) {

            if (numRows == 1) {
                return s;
            }

            List<StringBuilder> rows = new ArrayList<>();
            for (int i = 0; i < Math.min(numRows, s.length()); i++) {
                rows.add(new StringBuilder());
            }
            int curRow = 0;
            boolean goingDown = false;

            // 当我们向上移动到最上面的行或向下移动到最下面的行时，当前方向才会发生改变
            for (char c : s.toCharArray()) {
                rows.get(curRow).append(c);
                if (curRow == 0 || curRow == numRows - 1) {
                    goingDown = !goingDown;
                }
                curRow += goingDown ? 1 : -1;
            }

            StringBuilder ret = new StringBuilder();
            for (StringBuilder row : rows) {
                ret.append(row);
            }
            return ret.toString();
        }
    }
}
